package google.controllers;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailServiceFactory;

import engine.content.ContentTypes;
import engine.content.IContent;
import engine.dataobjects.Image;
import engine.dataobjects.MailItem;
import engine.dataobjects.MailItemInstance;
import engine.services.IContentService;
import engine.services.IDataResource;
import engine.services.IMailSender;
import engine.services.IMailService;
import engine.services.impl.GoogleImageService;
import engine.services.impl.GoogleMailSenderImpl;

@Controller
@RequestMapping("/")
public class TestController2 {
	
	@Autowired
	IMailSender mailSender;
	
	@Autowired
	IDataResource configResource;
	
	@Autowired
	IContentService contentService;
	
	@Autowired
	IMailService mailService;
	

	@RequestMapping(value="/mailitems", method=RequestMethod.GET)
	public @ResponseBody List<MailItem> getItems() throws IOException {
		try {
			this.configResource.refresh();		
			return this.configResource.getAllMailItems();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@RequestMapping(value="/mailitem/show/{id}", method=RequestMethod.GET)
	public void getItem(@PathVariable(value = "id") String id, HttpServletResponse response) throws IOException {
		try {
			MailItem item = getItemById(id);
			if (id == null) {
				throw new RuntimeException("ID "+id+" not found.");
			}
			IContent content = contentService.getResponse(item);
			response.setContentType(content.getContentType());
			
			if (content.getType() == ContentTypes.FILE || content.getType() == ContentTypes.MEMORY) {
				OutputStream out = response.getOutputStream();
				out.write((byte[])content.getContent());
			} else {
				response.getWriter().write(content.getContentAsString());							
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@RequestMapping(value="/mailitem/mail/{id}", method=RequestMethod.GET)
	public void mailItem(@RequestParam(value="email",required=true) String emailAddress, @PathVariable(value = "id") String id, HttpServletResponse response) throws IOException {
		try {
			MailItem item = getItemById(id);
			if (item == null) {
				throw new RuntimeException("ID "+id+" not found.");
			}
			MailItemInstance instance = new MailItemInstance(item);
			List<String> list = new ArrayList<>();
			list.add(emailAddress);
			instance.setTo(list);
			
			IContent content = contentService.getResponse(item);
			
			MailService mailService = MailServiceFactory.getMailService();
			MailService.Message message = new MailService.Message();
			message.setSender("ilana.paktor@gmail.com");
			message.setSubject("title");
			message.setTo(emailAddress);
			message.setHtmlBody("<HTML><img src='cid:pic.jpg'></HTML>");
			MailService.Attachment attachment =
			        new MailService.Attachment("pic.jpg",(byte[])content.getContent());
			message.setAttachments(attachment);
			mailService.send(message);
			

			//mailService.mail(instance);
			response.getWriter().write("Check your inbox");							
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void setMailService(IMailService mailService) {
		this.mailService = mailService;
	}

	public void setContentService(IContentService contentService) {
		this.contentService = contentService;
	}

	private MailItem getItemById(String id) {
		for (MailItem i : this.configResource.getAllMailItems()) {
			if (i.getId().equals(id)) {
				return i;
			}
		}
		return null;
	}
	
	public void setMailSender(IMailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	public void setConfigResource(IDataResource configResource) {
		this.configResource = configResource;
	}

}
